package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.text.SimpleDateFormat;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class CustomVM extends TitleVM {
    public CustomVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("自定义日程");

    }

    public ObservableField<String> scheduleType = new ObservableField<>("自定义");
    public ObservableField<String> title = new ObservableField<>();
    public ObservableField<String> Text = new ObservableField<>();
    public ObservableField<String> scheduleTime = new ObservableField<>();
    public ObservableField<String> remind = new ObservableField<>();
    public ObservableField<String> housingID = new ObservableField<>();
    public ObservableField<String> customid = new ObservableField<>();
    public ObservableField<String> circulateNum = new ObservableField<>();
    public ObservableField<String> circulateTime = new ObservableField<>();
    public ObservableField<String> circulateTemind = new ObservableField<>();

    public BindingCommand selTime = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            TimePickerView pvTime = new TimePickerBuilder(mActivity, (date, v) -> {
                scheduleTime.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
            })
                    .setType(new boolean[]{true, true, true, true, true, true})
                    .setLabel("年", "月", "日", "时", "分", "秒")
                    .build();
            pvTime.show();
        }
    });

    public BindingCommand selTime1 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            TimePickerView pvTime = new TimePickerBuilder(mActivity, (date, v) -> {
                circulateTime.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
            })
                    .setType(new boolean[]{true, true, true, true, true, true})
                    .setLabel("年", "月", "日", "时", "分", "秒")
                    .build();
            pvTime.show();
        }
    });

    public BindingCommand postData= new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (ValidateUtils.isEmptyString(title.get())){
                ToastUtils.showShort("请输入标题");
                return;
            }
            if (ValidateUtils.isEmptyString(Text.get())){
                ToastUtils.showShort("请输入内容");
                return;
            }
            if (ValidateUtils.isEmptyString(scheduleTime.get())){
                ToastUtils.showShort("请选择跟进时间");
                return;
            }

            saveData();

        }
    });

    private void saveData() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("scheduleType",scheduleType.get());
        map.put("title",title.get());
        map.put("Text",Text.get());
        map.put("scheduleTime",scheduleTime.get());
        map.put("remind",remind.get());
        map.put("housingID",housingID.get());
        map.put("customid",customid.get());
        map.put("circulateNum",circulateNum.get());
        map.put("circulateTime",circulateTime.get());
        map.put("circulateTemind",circulateTemind.get());

        model.addschedule(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            ToastUtils.showShort(o.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
